package com.lzjn.code.web.controller;

import com.lzjn.code.common.constant.Constants;
import com.lzjn.code.common.model.JsonModel;
import com.lzjn.code.common.model.PageModel;
import com.lzjn.code.web.dto.TableConfigSearchDto;
import com.lzjn.code.web.entity.DataSource;
import com.lzjn.code.web.entity.TableConfig;
import com.lzjn.code.web.service.DataSourceService;
import com.lzjn.code.web.service.TableConfigService;
import com.lzjn.code.web.util.ReadTableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/tableConfig")
public class TableConfigController {

	private static Logger log = LoggerFactory.getLogger(TableConfigController.class);
	
	@Autowired
	private DataSourceService dataSourceService;
	@Autowired
	private TableConfigService tableConfigService;
	
	@GetMapping("/init")
	public String init(){
		
		return "views/tableConfig/tableConfigList";
	}
	
	@PostMapping("/getData")
	@ResponseBody
	public PageModel getData(TableConfigSearchDto tableConfigSearchDto, PageModel page){
		
		try {
			
			TableConfig tableConfig = new TableConfig();
			tableConfig.setTableName(tableConfigSearchDto.getTableName());
			tableConfig.setDescription(tableConfigSearchDto.getDescription());
			
			page = tableConfigService.getByPage(tableConfig, page);
		} catch (Exception e) {
			
			log.error(e.getMessage(), e);
		}
		
		return page;
	}
	
	@GetMapping("/tableInit")
	public String tableInit(HttpServletRequest request){
	
		try {
			
			List<DataSource> dataSourceList = dataSourceService.getList();
			request.setAttribute("dataSourceList", dataSourceList);
		} catch (Exception e) {
			
			log.error(e.getMessage(), e);
		}
		
		return "views/tableConfig/tableConfigAdd";
	}
	
	@PostMapping("/getTableList")
	@ResponseBody
	public JsonModel getTableList(String dataSourceId){
		
		JsonModel json = new JsonModel();
		
		try {
			
			DataSource dataSource = dataSourceService.getById(dataSourceId);
			List<String> tableList = new ReadTableUtil().readAllTableNames(dataSource);
			json.setData(tableList);
		} catch (Exception e) {
			
			json.setSuccess(false);
			json.setMessage("连接数据库异常，请检查连接信息是否正确");
			log.error(e.getMessage(), e);
		}
		
		return json;
	}
	
	@GetMapping("/tableConfig")
	public String tableConfig(String id, HttpServletRequest request){
		
		try {
			
			TableConfig tableConfig = tableConfigService.getById(id);
			request.setAttribute("tableConfig", tableConfig);
		} catch (Exception e) {
			
			log.error(e.getMessage(), e);
		}
		
		return "views/tableConfig/tableConfigEdit";
	}
	
	@PostMapping("/update")
	@ResponseBody
	public JsonModel update(TableConfig tableConfig){
		
		JsonModel json = new JsonModel();
		
		try {
			
			json.setMessage(tableConfigService.update(tableConfig));
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMessage(Constants.MESSAGE_SAVE_FAIL);
			log.error(e.getMessage(), e);
		}
		
		return json;
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public JsonModel delete(String ids){
		
		JsonModel json = new JsonModel();
		
		try {
			
			json.setMessage(tableConfigService.delete(ids));
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMessage("remove_fail");
			log.error(e.getMessage(), e);
		}
		
		return json;
	}
}
